package com.dashan.backend.datastore;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.dashan.backend.XmlSerializable;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DsStatus implements Cloneable, XmlSerializable {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent
    private String userName;

    @Persistent
    private Date timestamp;
    
    @Persistent
    private String message;
    
    // Constructors.
    public DsStatus(String userName, String message, Date timestamp) {
        this.userName = userName;
        this.message = message;
        this.timestamp = timestamp;
    }
    
    // Getters and Setters.
    
    public String getUserName() {
        return userName;
    }
    
    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public Key getKey() {
        return key;
    }
    
    public Element getXmlElement(Document xmlDoc) {
        Element root = xmlDoc.createElement("status");
        
        if (getMessage() != null && getTimestamp() != null) {
            Element ele = null;
            ele = xmlDoc.createElement("message");
            ele.appendChild(xmlDoc.createTextNode(getMessage()));
            root.appendChild(ele);
            ele = xmlDoc.createElement("timestamp");
            ele.appendChild(xmlDoc.createTextNode(getTimestamp().toString()));
            root.appendChild(ele);
        }
        
        return root;
    }
}
